package com.yanqu.road.dataHelper.action.excel;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dataHelper.gameServer.GameServer;
import com.yanqu.road.dataHelper.repair.BaseRepair;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.activity.UserActivityConditionData;
import com.yanqu.road.entity.union.UnionBaseInfo;
import com.yanqu.road.entity.union.UnionInfo;
import com.yanqu.road.utils.string.StringUtils;

import java.math.BigInteger;
import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class UnionActivityServerMove extends BaseRepair {

    public UnionActivityServerMove(GameServer gameServer) {
        super(gameServer);
    }

    @Override
    public void repair(long param1, String param2) {

    }

    public Map<Long, UserActivityConditionData> getUserActivityConditionData(int activityId, Integer conditionType) {

        Map<Long, UserActivityConditionData> dataMap = new ConcurrentHashMap<>();
            String sql = "select * from t_u_activity_condition where  activityId  = ? and type = ?";


        ResultSet rs = null;
        CallableStatement call = null;

        try {
            call = getGameServer().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, conditionType);
            rs = call.executeQuery();
            while (rs.next()) {
                UserActivityConditionData data = parseData(rs);
                dataMap.put(data.getUserId(), data);
            }
        } catch (SQLException ex) {
            getGameServer().getLogger().error("异常错误", ex);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return dataMap;
    }


    protected UserActivityConditionData parseData(ResultSet rs) throws SQLException {
        UserActivityConditionData data = new UserActivityConditionData();
        data.setUserId(rs.getLong("userId"));
        data.setActivityId(rs.getInt("activityId"));
        data.setConditionId(rs.getInt("conditionId"));
        String value = rs.getString("preValue");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setPreValue(new BigInteger(value));
        }
        value = rs.getString("value");
        if(!StringUtils.isNullOrEmpty(value)) {
            data.setValue(new BigInteger(value));
        }
        data.setGetReward(rs.getBoolean("isGetReward"));
        data.setModifyTime(rs.getLong("modifyTime"));
        data.setParam(rs.getString("param"));
        data.setType(rs.getInt("type"));
        return data;
    }

    public List<UnionActivityConditionData> getUnionConditionData(int activityId) {
        List<UnionActivityConditionData> list = new ArrayList<>();
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_union_activity_condition where activityId = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            call.setInt(1, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionActivityConditionData data = new UnionActivityConditionData();
                data.setUnionUid(rs.getString("unionUid"));
                data.setActivityId(rs.getInt("activityId"));
                data.setConditionId(rs.getInt("conditionId"));
                String value = rs.getString("value");
                if(!StringUtils.isNullOrEmpty(value)) {
                    data.setValue(new BigInteger(value));
                }
                value = rs.getString("preValue");
                if(!StringUtils.isNullOrEmpty(value)){
                    data.setPreValue(new BigInteger(value));
                }
                data.setGetReward(rs.getBoolean("isGetReward"));
                data.setLastUpdateTime(rs.getLong("lastUpdateTime"));
                data.setConditionType(rs.getInt("conditionType"));
                data.setMemberUserIdList(StringUtils.stringToLongList(rs.getString("memberList"), ","));
                data.setInfo(rs.getString("info"));
                value = rs.getString("unionBaseInfo");
                if(null == value) {
                    value = "";
                }
                UnionBaseInfo unionBaseInfo = JSON.parseObject(value, new TypeReference<UnionBaseInfo>() {});
                if(null != unionBaseInfo){
                    data.setUnionBaseInfo(unionBaseInfo);
                }

                String position =  rs.getString("positionInfo");
                if(StringUtils.isNullOrEmpty(position)) {
                    position = "{}";
                }
                HashMap<Long, Integer> positionMap = JSON.parseObject(position, new TypeReference<HashMap<Long, Integer>>() {
                });
                if(null != positionMap) {
                    data.setMemberPositionMap(positionMap);
                }
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getGameServer().closeConnection(call, rs);
        }
        return list;
    }

    public List<UserActivityConditionData> getCommonUserActivityConditionData(int activityId, Integer conditionType) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<UserActivityConditionData> list  = new ArrayList<>();
            String sql = "select * from t_u_common_activity_condition where activityId = ? and type = ?" ;
            try {
                call =  getGameServer().getConnection().prepareCall(sql);
                call.setInt(1, activityId);
                call.setInt(2, conditionType);
                rs = call.executeQuery();
                while (rs.next()) {
                    UserActivityConditionData data = new UserActivityConditionData();
                    data.setUserId(rs.getLong("userId"));
                    data.setActivityId(rs.getInt("activityId"));
                    data.setConditionId(rs.getInt("conditionId"));
                    String value = rs.getString("preValue");
                    if(!StringUtils.isNullOrEmpty(value)) {
                        data.setPreValue(new BigInteger(value));
                    }
                    value = rs.getString("value");
                    data.setValue(new BigInteger(value));
                    data.setGetReward(rs.getBoolean("isGetReward"));
                    data.setModifyTime(rs.getLong("modifyTime"));
                    data.setType(rs.getInt("type"));
                    list.add(data);
                }
            } catch (Exception e) {
                throw new RuntimeException(e);
            } finally {
                getGameServer().closeConnection(call, rs);
            }
        return list;
    }

    public UnionInfo getUnionInfo(String unionUid) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_u_union where unionUid = ?";
        try {
            call = getGameServer().getConnection().prepareCall(sql);
            call.setString(1, unionUid);
            rs = call.executeQuery();
            while (rs.next()) {
                UnionInfo data = new UnionInfo();
                data.setUnionUid(rs.getString("unionUid"));
                data.setUnionName(rs.getString("unionName"));
                data.setCreateTime(rs.getLong("createTime"));
                data.setOutNotice(rs.getString("outNotice"));
                data.setInnerNotice(rs.getString("innerNotice"));
                data.setExperience(rs.getLong("experience"));
                data.setMasterUserId(rs.getLong("ownerUserId"));
                data.setMasterNickName(rs.getString("ownerNickName"));
                data.setMasterJoinTime(rs.getLong("ownerJoinTime"));
                data.setWechatCode(rs.getString("wechatCode"));
                data.setQqCode(rs.getString("qqCode"));
                data.setMemberCount(rs.getInt("memberCount"));
                data.setAllowRandomJoin(rs.getBoolean("isAllowRandomJoin"));
                String value = rs.getString("totalEarnSpeed");
                if(!StringUtils.isNullOrEmpty(value)){
                    data.setTotalEarnSpeed(new BigInteger(value));
                }
                data.setWealth(rs.getLong("wealth"));
                data.setDelete(rs.getBoolean("isDelete"));
                data.setModifyTime(rs.getLong("modifyTime"));
                data.setUnionFightRewardMark(rs.getInt("unionFightRewardMark"));
                data.setUnionFlag(rs.getInt("unionFlag"));
                data.setFlagModifyTime(rs.getLong("flagModifyTime"));
                data.setConstructActive(rs.getInt("constructActive"));
                data.setVipExp(rs.getLong("vipExp"));
                data.setConstructUserIdList(StringUtils.stringToLongList(rs.getString("constructUserIdList"), ","));
                data.setUnionAliasId(rs.getLong("unionAliasId"));
                value = rs.getString("decoration");
                if(!StringUtils.isNullOrEmpty(value)){
                    data.setDecoration(value);
                }
                data.setRankActivityId(rs.getInt("rankActivityId"));
                data.setRankAddExp(rs.getLong("rankAddExp"));
                data.setPreUnionFlag(rs.getInt("preUnionFlag"));
                data.setDayJoinNum(rs.getInt("dayJoinNum"));
                return data;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
           getGameServer().closeConnection(call, rs);
        }
        return null;
    }
}
